package sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class SuppliersDetails {
	public ArrayList<String>  getdetails() throws SQLException
	{
		ArrayList<String> arr=new ArrayList<String>();
		try 
		{
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			
			ResultSet rs= stmt.executeQuery("SELECT * FROM mtl.supplier ;");
				while (rs.next())
				{
					
					arr.add(rs.getString(1));
					arr.add(rs.getString(2));
					arr.add(rs.getString(3));
					arr.add(rs.getString(4));
					arr.add(rs.getString(5));
					arr.add(rs.getString(6));
					arr.add(rs.getString(7));
					arr.add(rs.getString(8));
					arr.add(rs.getString(9));
					
				}
			return arr;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return null;
		
		
	}
	public ArrayList<String>  getSupNamesList() throws SQLException
	{
		ArrayList<String> arr=new ArrayList<String>();
		try 
		{
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			
			ResultSet rs= stmt.executeQuery("SELECT mtl.supplier.name FROM mtl.supplier ;");
				while (rs.next())
				{
					arr.add(rs.getString(1));
				}
			return arr;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return null;
		
		
	}
	public int adduser(ArrayList<String> suparr) {
		try 
		{
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			stmt.executeUpdate("insert into mtl.supplier(name,country,city,address,tel,fax,email,account) values('"+suparr.get(0)+"','"+suparr.get(1)+"','"+suparr.get(2)+"','"+suparr.get(3)+"','"+suparr.get(4)+"','"+suparr.get(5)+"','"+suparr.get(6)+"','"+suparr.get(7)+"');");
				
			return 1;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return 0;
	}

	public ArrayList<String> getSupplierDetails(String supid) {
		ArrayList<String> arr=new ArrayList<String>();
		 Statement stmt;
			SQLConnection conn = new  SQLConnection();
			
				try {
					stmt = conn.getConnection().createStatement();
				
				ResultSet rs= stmt.executeQuery("Select * from mtl.supplier where sup_id = '"+supid+"';");
				while (rs.next())
				{
				arr.add(rs.getString(1));
				arr.add(rs.getString(2));
				arr.add(rs.getString(3));
				arr.add(rs.getString(4));
				arr.add(rs.getString(5));
				arr.add(rs.getString(6));
				arr.add(rs.getString(7));
				arr.add(rs.getString(8));
				arr.add(rs.getString(9));
				}
			return arr;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
			}

	public int suppliersDetailsUpdate(ArrayList<String> strspd) {
		Statement stmt1;
		
		SQLConnection conn = new  SQLConnection();
		try {
			stmt1 = conn.getConnection().createStatement();
			stmt1.executeUpdate("update  mtl.supplier set name = '"+strspd.get(0)+"',country = '"+strspd.get(1)+"',city = '"+strspd.get(2)+"', address = '"+strspd.get(3)+"', tel = '"+strspd.get(4)+"', fax = '"+strspd.get(5)+"',email = '"+strspd.get(6)+"',account = '"+strspd.get(7)+"' where sup_id = '"+strspd.get(8)+"';");
			return 1;
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public int deleteSupplierDB(String id) {

		try 
		{

			
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			System.out.println("deleting");
			stmt.executeUpdate("delete from mtl.supplier where sup_id = '"+id+"';");
			return 1;
		
		}
  
		catch (Exception ex )
		{
				ex.printStackTrace();
		}
		return 0;
		
	}
	

}


	
